import socket
import threading
import queue
import serverFunc
# 创建TCP/IP套接字
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# a queue to safe the info from client
infoList = queue.Queue()
respList = queue.Queue()
isactive = threading.Lock()
# keep the thread tongbu
hasInfo = threading.Event()
hasResp = threading.Event()
# 绑定IP地址和端口号
server_address = ('0.0.0.0', 9001)
server_socket.bind(server_address)

# 监听连接请求
server_socket.listen(5)

print('服务器已启动，等待连接...')



while True:
    # 等待客户端连接
    client_socket, client_address = server_socket.accept()
    typ = client_socket.recv(1024)
    try:
        print("connect type: " + typ.decode("utf-8") + ",connect")
        if typ.decode("utf-8") == "aliyun":
            client_thread = threading.Thread(target=serverFunc.aliyun_auto_Complementary, args=(client_socket, client_address))
            client_thread.start()
        elif typ.decode("utf-8") == "client":
            client_thread = threading.Thread(target=serverFunc.client_from_database, args=(client_socket, client_address))
            client_thread.start()
    except UnicodeDecodeError:
        print("unknown access!")
        continue
